package org.streets.eis.entity;

import java.sql.ResultSet;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.wicket.IClusterable;
import org.apache.wicket.util.string.Strings;
import org.streets.commons.util.JDBCUtils;
import org.streets.commons.util.StringUtils;
import org.streets.database.Limits;
import org.streets.database.PV;
import org.streets.database.SQLConnection;
import org.streets.database.SQLNestedException;
import org.streets.wicket.HiveWebApplication;
/**
 * 
 * @author dzb
 */
@Entity @Table(name="EIS_USER")
public class User implements IClusterable {
	private static final long serialVersionUID = 1L;
	
	@Id @Column(name="user_id")
    private String uid;
	
	@Column(name="pass")
    private String passwd;

	@Column(name="user_level")
    private Integer level;
	
    private String roleCodes;
    
    private String roleIds;

	public User() {
		
	}


    /**
     * Whether this user has the given role.
     *
     * @param role
     * @return whether this user has the given role
     */
    public boolean hasRoleCode(String roleCode) {
    	
        return StringUtils.contains(roleCodes, roleCode);
    }

    /**
     * Gets the uid.
     *
     * @return the uid
     */
    public String getUid() {
        return uid;
    }
    
    public void setUid(String id) {
    	this.uid = id;
    }
    
	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}
	
	public String getRoleCodes() {
		if (Strings.isEmpty(roleCodes)){
			SQLConnection conn = HiveWebApplication.get().getRegistry().service(SQLConnection.class);
			String sql = "SELECT CODE FROM EIS_ROLES WHERE ID IN"+
					"(SELECT ROLE_ID FROM EIS_POS_ROLE_MAP WHERE POS_ID ="+
					"	(SELECT POS_ID FROM EIS_EMPLOYEE WHERE USER_ID=?))";
			ResultSet rs = conn.query(sql,Limits.none(), PV.of(uid));
			try{	
				StringBuilder userRoles = new StringBuilder();
				while (rs.next()){
					userRoles.append(rs.getString(1)).append(",");
				}
				if (userRoles.length() > 0){
					userRoles.deleteCharAt(userRoles.length() - 1);
				}
				roleCodes = userRoles.toString();
			}catch(Exception e){
				throw new SQLNestedException(e);
			}finally{
				JDBCUtils.close(rs);
			}
		}
		return roleCodes;
	}

	public String getRoleIds() {
		if (Strings.isEmpty(roleIds)){
			SQLConnection conn = HiveWebApplication.get().getRegistry().service(SQLConnection.class);
			String sql = "SELECT ROLE_ID FROM EIS_POS_ROLE_MAP WHERE POS_ID ="+
					"	(SELECT POS_ID FROM EIS_EMPLOYEE WHERE USER_ID=?)";
			ResultSet rs = conn.query(sql,Limits.none(), PV.of(uid));
			try{	
				StringBuilder userRoles = new StringBuilder();
				while (rs.next()){
					userRoles.append(rs.getString(1)).append(",");
				}
				if (userRoles.length() > 0){
					userRoles.deleteCharAt(userRoles.length() - 1);
				}
				roleIds = userRoles.toString();
			}catch(Exception e){
				throw new RuntimeException(e);
			}finally{
				JDBCUtils.close(rs);
			}
		}
		return roleIds;
	}
	
	public void setRoleCodes(String roleCodes) {
		this.roleCodes = roleCodes;
	}
	
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return uid + " " + roleCodes;
    }
}
